package JAVA300.第七章数组;

import java.util.Arrays;

public class TestBinarySearch {
    public static void main(String[] args) {
        int[] arr ={3,7,2,9,5};

        System.out.println("索引值为"+MyBinarySearch(arr,5));
        System.out.println(Arrays.toString(arr));
    }

    public static int MyBinarySearch(int[] arr,int num){

        Arrays.sort(arr);
        int low = 0;
        int high = arr.length-1;

        while (arr[low]< arr[high]){//

            int mid = (low+high)/2;

            if (arr[mid] == num){
                return mid;
            }

            if (arr[mid] > num){
                high = mid-1;
            }

            if(arr[mid] < num){
                low = mid-1;
            }

        }

        return -1;
    }
}
